package com.iedu.demo.lgq.controller;

import com.iedu.demo.lgq.entity.TbCategory;
import com.iedu.demo.lgq.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/categories")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    @GetMapping("/all")
    public List<TbCategory> getAllCategories() {
        return categoryService.getAllCategories();
    }

    @GetMapping("/getSubCategories/{parentId}")
    public List<TbCategory> getSubCategories(@PathVariable Long parentId) {
        return categoryService.getSubCategoriesByParentId(parentId);
    }

    @GetMapping("/{categoryId}")
    public TbCategory getCategory(@PathVariable Long categoryId) {
        return categoryService.getCategoryById(categoryId);
    }

    @PostMapping("/create")
    public int createCategory( TbCategory category) {
        return categoryService.insertCategory(category);
    }

    @PutMapping("/update/{categoryId}")
    public int updateCategory(@PathVariable Long categoryId, @RequestBody TbCategory category) {
        category.setCategoryId(categoryId);
        return categoryService.updateCategory(category);
    }

    @DeleteMapping("/delete/{categoryId}")
    public int deleteCategory(@PathVariable Long categoryId) {
        return categoryService.deleteCategory(categoryId);
    }
}